package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.base.utils.CacheUtil;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboInsertParam;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Validated
@Slf4j
@RestController
@Api(tags = "02.微博模块")
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private CacheUtil cacheUtil;

    @PostMapping("insert")
    @ApiOperation("发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeibo(@RequestBody WeiboInsertParam weiboInsertParam, @ApiIgnore HttpSession session){
        log.debug("weiboInsertParam = " + weiboInsertParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboInsertParam, weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        log.debug("weibo = " + weibo);
        weiboMapper.insertWeibo(weibo);

        return JsonResult.ok();
    }

    /**
     * 2.微博首页列表功能
     */
    @GetMapping("selectIndex")
    @ApiOperation("首页列表功能")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex(){
        Serializable weiboIndex = cacheUtil.get("weibo_index");
        if (weiboIndex != null){
            log.debug("首页数据来自Redis数据库...");
            return JsonResult.ok(weiboIndex);
        }
        log.debug("首页数据来自Mysql数据库...");
        List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndex();
        cacheUtil.setExpire("weibo_index", (Serializable) weiboIndexVOS, 30);
        return JsonResult.ok(weiboIndexVOS);
    }

    /**
     * 3.微博详情功能
     */
    @GetMapping("selectById")
    @ApiOperation("微博详情功能")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(@Range(min = 100, max = 500, message = "微博编号必须在100-500之间") @RequestParam Integer id){
        log.debug("id = " + id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);

        return JsonResult.ok(weiboDetailVO);
    }
}
